package com.cc.data_explore.gui.sheet;

import com.cc.data_explore.gui.base.BaseView;
import de.saxsys.mvvmfx.FxmlView;
import de.saxsys.mvvmfx.InjectViewModel;
import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableMap;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * SheetDataView
 *
 * @author aa
 * @version 1.0
 * @since 2025/3/31
 */
public class SheetDataDialogView extends BaseView implements FxmlView<SheetDataDialogViewModel> {

    @FXML
    public TableView<Map<String, String>> sheetTable;

    @InjectViewModel
    private SheetDataDialogViewModel viewModel;

    private final ObservableMap<String, String> columnMap = FXCollections.observableMap(new LinkedHashMap<>());


    @FXML
    public void initialize() {
        sheetTable.setItems(viewModel.getItems());
        Bindings.bindContent(columnMap, viewModel.getColumnMap());
        viewModel.subscribe(SheetDataDialogViewModel.COLUMN_CHANGE, (key, payload) -> setCol());
    }

    public void setCol() {
        columnMap.forEach((k, v) -> {
            TableColumn<Map<String, String>, String> categoryCol = new TableColumn<>(v);
            categoryCol.setCellValueFactory(param -> new SimpleStringProperty(param.getValue().get(k)));
            sheetTable.getColumns().add(categoryCol);
        });
    }
}
